package com.zzptc.zzsgz.bdsjws;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.socks.library.KLog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zzptc.zzsgz.bdsjws.com.Constants;
import com.zzptc.zzsgz.bdsjws.db.BBXDao;
import com.zzptc.zzsgz.bdsjws.db.DaoMaster;
import com.zzptc.zzsgz.bdsjws.db.DaoSession;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;
import com.zzptc.zzsgz.bdsjws.utils.SelectContacts;
import com.zzptc.zzsgz.bdsjws.vo.BBX;
import com.zzptc.zzsgz.bdsjws.vo.Contacts;

import org.greenrobot.greendao.database.Database;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by zzsgz on 2017/4/20.
 */

public class MyApplication extends Application {
    //功能：1、提供全局的上下文 2、初始化KLOG
    static Context context;
    static DaoSession daoSession;
    static DaoSession daoMobileSession;
    public static List<Contacts> contacts_list;
    public static HashMap<String,Contacts> checked_contacts_hashMap = new HashMap<>();

//真正的入口方法
    @Override
    public void onCreate() {
        super.onCreate();
        context=getApplicationContext();
        KLog.init(BuildConfig.LOG_DEBUG, Constants.LOG_FLAG);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();
        OkHttpUtils.initClient(okHttpClient);
        initDatabase();
        initMobileDatabase();
        initDatatoTable();
        contacts_list= SelectContacts.select(context);
    }

    public static Context getContext(){

        return context;
    }
    private static void initDatabase(){
        DaoMaster.DevOpenHelper  openHelper=new DaoMaster.DevOpenHelper(context,Constants.DATABASE_NAME);
        Database database=openHelper.getWritableDb();
        DaoMaster daoMaster=new DaoMaster(database);
        daoSession=daoMaster.newSession();
    }

    public static DaoSession getDaoSession(){
        return daoSession;
    }

    private static void initMobileDatabase(){
        DaoMaster.DevOpenHelper  openHelper=new DaoMaster.DevOpenHelper(context,"mobile");
        Database database=openHelper.getWritableDb();
        DaoMaster daoMaster=new DaoMaster(database);
        daoMobileSession=daoMaster.newSession();
    }

    public static DaoSession getMobiledaoSession(){
        return daoMobileSession;
    }

    public void initDatatoTable(){
        DaoSession daoSession=getDaoSession();
        BBXDao bbxDao=daoSession.getBBXDao();

        SharedPreferences sharedPreferences= MyUtil.getSharedPre();
        boolean b=sharedPreferences.getBoolean("inserttable",true);
        if (b){
            String[] names=getResources().getStringArray(R.array.bbx_name);
            String[] icons=getResources().getStringArray(R.array.bbx_icon);
            try {
                MyUtil.copyFileToMobile(context,"mobile.db","mobile");
            } catch (IOException e) {
                e.printStackTrace();
            }
            for (int i=0;i<names.length;i++){
                BBX bbx=new BBX(null,names[i],icons[i]);
                try {
                    long m=bbxDao.insert(bbx);
                    if (m>0){
                        KLog.i("新增成功");
                    }else{
                        KLog.i("新增失败");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putBoolean("inserttable",false);
            editor.commit();
        }else{

        }

    }

    public static List<Contacts> getChecked_contacts_list(){
        List<Contacts> checked_contacts_list = new ArrayList<>();
        Iterator iter = checked_contacts_hashMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = (String) entry.getKey();
            System.out.println(key+"----map");
            Contacts val = (Contacts) entry.getValue();
            checked_contacts_list.add(val);
        }
        return checked_contacts_list;
    }

}
